package com.hsxc.cdz.mgmt.pile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hsxc.cdz.base.bean.Pile;
import com.hsxc.cdz.base.bean.Station;
import com.hsxc.cdz.base.dao.PileDao;
import com.hsxc.cdz.base.dao.StationDao;

@Component
public class PileManager {

    private Logger logger = LoggerFactory.getLogger(PileManager.class);
    @Autowired
    private PileDao pileDao;

    //手工添加充电桩信息
    public int addPileManually(Pile pile) {
        return pileDao.insertRegister(pile);
    }

    //手工删除充电桩信息
    public int delPileManually(String pileNO) {
        Pile pile = new Pile();
        pile.setPileNO(pileNO);
        return pileDao.deletePile(pile);
    }

    //充电桩登录接入系统（注册、再次登录都是这个接口）
    public int pileLogin(boolean registered) {
        return 0;
    }

    //通过充电桩设备地址查找桩
    public Pile selPile(String pileNO) {
        Pile pile = new Pile();
        pile.setPileNO(pileNO);
        return pileDao.selectPile(pile);
    }

    //通过用户电话号码查看已经分享的桩
    public List<Pile> selPileByTel(String tel) {
        Pile pile = new Pile();
        pile.setOwnType(Pile.OWN_TYPE_PRIV);
        pile.setTel(tel);
        return pileDao.selectPileByTel(pile);
    }

    //根据电站编号查看电桩
    public List<Pile> getPilesByStationNo(String stationNo){
        Pile pile = new Pile();
        pile.setStationNO(stationNo);
        return pileDao.getPilesByStationNo(pile);
    }
    //充电桩登录更新数据库数据
    public int login(Pile pile) {
        return pileDao.updatePile(pile);
    }

    /**
     * 更新单个电桩的数据
     *
     * @return 1：更新成功  0：更新失败
     */
    public int updatePileByPage(Pile pile) {
        pile.setState(6);
        return pileDao.updatePile(pile);
    }

    public int updatePile(Pile pile) {
        return pileDao.updatePile(pile);
    }

    public List<Pile> getAllPile() {
        Pile pile = new Pile();
        pile.setId(0);
        pile.setStationNO(null);
        pile.setAccount(null);
        pile.setPassword(null);
        pile.setOperatorNO(null);
        pile.setOwnType(1);
        pile.setTel(null);
        return pileDao.selectPiles(pile);
    }

    public List<Pile> getpileByStation(String stationNO) {
        Pile pile = new Pile();
        pile.setStationNO(stationNO);
        return pileDao.selectPiles(pile);
    }

    public List<Map<String, Object>> getByLatlong(double[] range) {
        Map<String, Double> map = new HashMap<>();
        map.put("minLat", range[0]);
        map.put("maxLat", range[1]);
        map.put("minLong", range[2]);
        map.put("maxLong", range[3]);
        return pileDao.selByLatlong(map);
    }

    /**
     * 根据运营商编号查找
     *
     * @param operatorNO
     * @return
     */
    public List<Pile> getpileByOperatorNO(String operatorNO) {
        List<Pile> piles = null;
        Pile pile = new Pile();
        if (operatorNO != null) {
            pile.setOperatorNO(operatorNO);
            piles = pileDao.selectPiles(pile);
        }
        return piles;
    }

    /**
     * 根据电站管理员查找
     */
    public List<Pile> getpileByStationNO(String stationno) {
        List<Pile> piles = null;
        Pile pile = new Pile();
        if (!stationno.equals(null)) {
            pile.setStationNO(stationno);
            piles = pileDao.selectPiles(pile);
        }
        return piles;
    }

    /**
     * 根据电站编号查找电桩
     */
    public List<Pile> getPileByStationNO(List<String> stationNO) {
        List<Pile> pile = new ArrayList<>();
        if (!stationNO.equals(null)) {
            for (Iterator<String> iterator = stationNO.iterator(); iterator.hasNext(); ) {
                String stationno = iterator.next();
                pile = getpileByStationNO(stationno);
                pile.addAll(pile);
                logger.info("pile" + pile);
            }
        }
        return pile;
    }

    public List<Pile> searchPile(int pileType, String speed) {
        Pile pile = new Pile();
        pile.setOwnType(pileType);
        return pileDao.selectPiles(pile);
    }

    public int updateState(int state, String pileAddress) {
        Pile pile = new Pile();
        pile.setPileNO(pileAddress);
        pile.setState(state);
        return pileDao.updatePile(pile);
    }

}